import React, { PureComponent } from 'react';
import { Spin } from 'antd';
import { connect } from 'dva';
import BasicLayout from './BasicLayout';
import { getRouterData,formatterMenu,getFlatMenuData } from '../common/menu';
import CodeList from '../routes/List/companyPeople';
/**
 * 根据菜单取得重定向地址.
 */


let MenuData = [];
let RouterData = [];
const getRedirectData = () => {
  const redirectData = [];
  const getRedirect = item => {
    if (item && item.children) {
      if (item.children[0] && item.children[0].path) {
        redirectData.push({
          from: `${item.path}`,
          to: `${item.children[0].path}`,
        });
        item.children.forEach(children => {
          getRedirect(children);
        });
      }
    }
  };
  MenuData.forEach(getRedirect);
  return redirectData;
};
let redirectData = getRedirectData();

/**
 *  加载页面组件
 */
class LoadingPage extends PureComponent {
  state = {
    loading: true,
    isMobile: false,
  };

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'user/getUserInfo',
    });
    this.hideLoading();
  }

  hideLoading() {
    this.setState({
      loading: false,
    });
  }


  render() {
    const { loading, isMobile } = this.state;
    const { user,routerData} = this.props;
    if(user.menu && user.menu.length>0){
      MenuData = formatterMenu(user.menu);
      redirectData = getRedirectData();
      const FlatMenuData = getFlatMenuData(MenuData);
      RouterData = getRouterData(routerData,FlatMenuData);
    }
    if (loading) {
      return (
        <div
          style={{
            width: '100%',
            height: '100%',
            margin: 'auto',
            paddingTop: 50,
            textAlign: 'center',
          }}
        >
          <Spin size="large" />
        </div>
      );
    }




    return (
      <BasicLayout
        isMobile={isMobile}
        menuData={MenuData}
        redirectData={redirectData}
        routerconfig={RouterData}
        {...this.props}
      />
    );
  }
}

// 获取用户权限信息
export default connect(({ user, loading }) => ({
  user,
  loading: loading.effects['user/getUserInfo'],
}))(LoadingPage);
